package com.fxwater.admin.controller.biz;

import com.alibaba.fastjson.JSONObject;
import com.fxwater.biz.domain.ItemInfo;
import com.fxwater.biz.domain.PumpStationInfo;
import com.fxwater.biz.service.ItemService;
import com.fxwater.common.annotation.Log;
import com.fxwater.common.core.controller.BaseController;
import com.fxwater.common.core.domain.AjaxResult;
import com.fxwater.common.core.page.TableDataInfo;
import com.fxwater.common.enums.BusinessType;
import com.fxwater.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 监测项
 * @author ygz
 *
 */

@Controller
@RequestMapping("/biz/item")
public class ItemController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(PumpStationController.class);

    private String prefix = "biz/item";

    @Autowired
    private ItemService itemService;

    /**
     * 监测项数据信息列表
     * @param itemInfo
     * @return
     */
    @RequiresPermissions("biz:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list (ItemInfo itemInfo) {
        startPage();
        List<ItemInfo> list = itemService.selectItemInfoList(itemInfo);
        return getDataTable(list);
    }

    /**
     * 跳转页面
     * @return
     */
    @RequiresPermissions("biz:item:view")
    @GetMapping()
    public String item()
    {
        logger.info("监测项目");
        return prefix + "/item";
    }

    /**
     * 查看详情
     * @return
     */
    @GetMapping("/detail/unit/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap) {
        logger.info("====进入查看详情方法,传入的监测项id是:" + id + "====");
        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setId(Long.parseLong(id));
        mmap.put("itemInfo", itemService.selectItemInfoList(itemInfo).get(0));
        return prefix +"/detail";
    }

    /**
     * 跳转修改页面
     * @param mmap
     * @return
     */
    @GetMapping("edit/{itemId}")
    public String edit ( ModelMap mmap,@PathVariable("itemId") String itemId) {
        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setId(Long.parseLong(itemId));
        itemInfo =  itemService.selectItemInfoList(itemInfo).get(0);
        mmap.addAttribute("itemInfo", itemInfo);
        return prefix + "/edit";
    }

    /**
     * 保存监测项修改信息
     * @return
     */
    @RequiresPermissions("biz:item:edit")
    @Log(title = "监测项", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult editSave(ItemInfo itemInfo) {
        logger.info("监测项" + itemInfo);
        return toAjax(itemService.editSaveItemInfo(itemInfo));
    }

    /**
     * 增加监测项
     * @param mmap
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    /**
     * 添加监测项
     * @param itemInfo
     * @return
     */
    @RequiresPermissions("biz:item:add")
    @Log(title = "监测项新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ItemInfo itemInfo)
    {
        return toAjax(itemService.addItem(itemInfo));
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    @RequiresPermissions("biz::remove")
    @Log(title = "监测项删除", businessType = BusinessType.DELETE)
    public AjaxResult remove(String ids) {
        return toAjax(itemService.removeItem(ids));
    }

    /**
     * 修改息状态
     * @return
     */
    @RequestMapping("/changeStatus")
    @RequiresPermissions("biz:item:edit")
    @ResponseBody
    public AjaxResult changeStatus(String id,String status) {
        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setId(Long.parseLong(id));
        itemInfo.setIsValid(status);
        return toAjax(itemService.editSaveItemInfo(itemInfo));
    }

    /**
     * 根据code查询泵站信息
     *
     */
    @RequestMapping(value = "itemList",method = RequestMethod.POST,produces={"application/json"})
    @ResponseBody
    public List<Map<String, String>> stationList(@RequestBody JSONObject jsonParam){
        List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
        String pumpCode = jsonParam.getString("pumpcode");
        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setPumpCode(pumpCode);
        List<ItemInfo> itemInfoList = itemService.selectItemInfoByPumpCode(itemInfo);
        for (ItemInfo items : itemInfoList){
            Map<String, String> map = new HashMap<>();
            map.put("item_name", items.getItemName());
            map.put("item_code", items.getItemCode());
            resultList.add(map);
        }
        return resultList;
    }
}
